package com.example.treehole.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.treehole.R;
import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GridImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<LocalMedia> images=new ArrayList<>();
    private Context context;

    public GridImageAdapter(Context context,List<LocalMedia> images) {
        this.context=context;
        this.images=images;
    }

    public void setData(List<LocalMedia> images) {
        this.images = images;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic_insert_img, parent, false);
        return new LinerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        LinerViewHolder vh= (LinerViewHolder) holder;
        if (position>8){
            vh.ivCancel.setVisibility(View.GONE);
            return;
        }
        if (position != images.size()-1) {
            //前面的正常加载图片 picUrl为图片路径
            String picUrl = images.get(position).getPath();
            //Picasso.get().load("file://"+picUrl).resize(400,400).into(vh.ivShow);
            Picasso.get().load(new File(picUrl)).resize(400,400).into(vh.ivShow);
            vh.ivCancel.setVisibility(View.VISIBLE);
        } else{
            //最后一个显示添加图片按钮
            vh.ivShow.setImageResource(R.mipmap.insert);
            vh.ivCancel.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(position);
            }
        });

        vh.ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickCancel(position);
            }
        });
    }



    @Override
    public int getItemCount() {
        return images.size();
    }

    //接口
    public interface OnItemClickListener {
        void onClick(int pos);
        void onClickCancel(int pos);
    }

    private OnItemClickListener listener;

    public void setOnMyItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    static class LinerViewHolder extends RecyclerView.ViewHolder {
        ImageView ivShow;
        ImageView ivCancel;

        public LinerViewHolder(@NonNull View view) {
            super(view);
            ivShow=view.findViewById(R.id.iv_show);
            ivCancel=view.findViewById(R.id.iv_cancel);
        }
    }
}
